/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.dao.*;
import com.cashmate.hibernate.pojo.*;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Dine
 */
public class GetProfile extends ActionSupport {

    private static Logger logger = Logger.getLogger(GetAreas.class);
    private LoanapplicationDAO la = (LoanapplicationDAO) ServletActionContext.getServletContext().getAttribute("loanapplicationDAO");
    private MonthlyincomeDAO mid = (MonthlyincomeDAO) ServletActionContext.getServletContext().getAttribute("monthlyincomeDAO");
    private CreditinvestigationDAO cid = (CreditinvestigationDAO) ServletActionContext.getServletContext().getAttribute("creditinvestigationDAO");
    private AppraisalDAO apd = (AppraisalDAO) ServletActionContext.getServletContext().getAttribute("appraisalDAO");
    private LoansDAO ld = (LoansDAO) ServletActionContext.getServletContext().getAttribute("loansDAO");
    private List<Areas> areas;
    private String loanappid;
    private Loanapplications l;
    private Borrowers b;
    private Monthlyincome mi;
    private Creditinvestigations ci;
    private Appraisal ap;
    private List<Collaterals> listcoll;
    private List<Dependents> deplist;
    private List<Requirements> reqlist;
    private List<Characterreferences> reflist;
    private List<Loanswithotherbanks> loanlist;
    private List<Personalandrealproperties> parplist;
    private List<Sourceofincome> soilist;
    private List<Reputation> replist;
    private List<Notesreceivable> nrlist;
    private List<Informants> inflist;
    private List<Documentspassed> doclist;
    private List<Typeofcollateral> toclist;
    private Loans loans;

    public List<Dependents> getDeplist() {
        return deplist;
    }

    public List<Documentspassed> getDoclist() {
        return doclist;
    }

    public Loans getLoans() {
        return loans;
    }

    public List<Informants> getInflist() {
        return inflist;
    }

    public List<Loanswithotherbanks> getLoanlist() {
        return loanlist;
    }

    public List<Notesreceivable> getNrlist() {
        return nrlist;
    }

    public List<Personalandrealproperties> getParplist() {
        return parplist;
    }

    public List<Characterreferences> getReflist() {
        return reflist;
    }

    public List<Reputation> getReplist() {
        return replist;
    }

    public List<Requirements> getReqlist() {
        return reqlist;
    }

    public List<Sourceofincome> getSoilist() {
        return soilist;
    }

    public List<Typeofcollateral> getToclist() {
        return toclist;
    }

    public List<Areas> getAreas() {
        return areas;
    }

    public void setLoanappid(String loanappid) {
        this.loanappid = loanappid;
    }

    public Borrowers getB() {
        return b;
    }

    public Loanapplications getL() {
        return l;
    }

    public Monthlyincome getMi() {
        return mi;
    }

    public Creditinvestigations getCi() {
        return ci;
    }

    public Appraisal getAp() {
        return ap;
    }

    public List<Collaterals> getListcoll() {
        return listcoll;
    }

    @Action(value = "/get-profile", results = {
        @Result(name = "success", type = "redirect", location = "../../loanprofile.jsp")
    })
    public String getAreasList() {
//        System.out.println("BEFORE REMOVE: "+ServletActionContext.getRequest().getSession().getAttribute("colllist"));
        ServletActionContext.getRequest().getSession().removeAttribute("colllist");
        ServletActionContext.getRequest().getSession().removeAttribute("deplist");
        ServletActionContext.getRequest().getSession().removeAttribute("reqlist");
        ServletActionContext.getRequest().getSession().removeAttribute("reflist");
        ServletActionContext.getRequest().getSession().removeAttribute("loanlist");

        ServletActionContext.getRequest().getSession().removeAttribute("parplist");
        ServletActionContext.getRequest().getSession().removeAttribute("soilist");
        ServletActionContext.getRequest().getSession().removeAttribute("replist");
        ServletActionContext.getRequest().getSession().removeAttribute("nrlist");
        ServletActionContext.getRequest().getSession().removeAttribute("inflist");
        ServletActionContext.getRequest().getSession().removeAttribute("doclist");
        ServletActionContext.getRequest().getSession().removeAttribute("toclist");
//        System.out.println("AFTER REMOVE: "+ServletActionContext.getRequest().getSession().getAttribute("colllist"));
        try {
//            Branches b = (Branches) ServletActionContext.getRequest().getSession().getAttribute("branch");
//            areas = ar.getAreasPerBranch(b.getBranchid());
            boolean bool = true;
            l = la.get(Integer.parseInt(loanappid));
            b = l.getBorrowers();
            mi = mid.getLatestMi(Integer.parseInt(loanappid));
            ci = cid.getLatestByLoanId(Integer.parseInt(loanappid));
            ap = apd.getLatestByLoanId(Integer.parseInt(loanappid));
            listcoll = apd.getCollsPerAppraisalId(ap.getAppraisalid());
//            deplist = la.getDependentsByLoanapp(Integer.parseInt(loanappid));
            deplist = new ArrayList<Dependents>();
            List<LoanappRequirement> lar = la.getRequirementsByLoanapp(Integer.parseInt(loanappid));
            reflist = la.getReferencesByLoanapp(Integer.parseInt(loanappid));
            loanlist = la.getLoanswithotherByLoanapp(Integer.parseInt(loanappid));
            loans = ld.getLatestLoansByBorrower(b.getBorrowerid());
            List<Businesses> business = new ArrayList<Businesses>();
            Iterator ib = l.getBusinesseses().iterator();

            while (ib.hasNext()) {
                Businesses bu = (Businesses) ib.next();
                business.add(bu);
//                System.out.println(bu.getBusinessname());
            }

            //ci lists

            List<Vehicle> vehicles = new ArrayList<Vehicle>();
            if (ci.getVehicles() != null) {
                Iterator v = ci.getVehicles().iterator();
                while (v.hasNext()) {
                    Vehicle ve = (Vehicle) v.next();
                    vehicles.add(ve);
//                    System.out.println(ve.getVehicleid());
                }
            }


//            other source of income
            List<Othersourceofincome> osi = new ArrayList<Othersourceofincome>();
            if (ci.getOthersourceofincomes() != null) {
                Iterator o = ci.getOthersourceofincomes().iterator();
                while (o.hasNext()) {
                    Othersourceofincome other = (Othersourceofincome) o.next();
                    osi.add(other);
                    System.out.println(other.getType());
                }
            }


//            personal and real properties owned

//            List<Personalandrealproperties> parp = new ArrayList<Personalandrealproperties>();
            if (ci.getPersonalandrealpropertieses() != null) {
                parplist = new ArrayList<Personalandrealproperties>();
                Iterator p = ci.getPersonalandrealpropertieses().iterator();
                while (p.hasNext()) {
                    Personalandrealproperties per = (Personalandrealproperties) p.next();
                    parplist.add(per);
                }
            }


//            notes receivable

//            List<Notesreceivable> nr = new ArrayList<Notesreceivable>();
            if (ci.getNotesreceivables() != null) {
                nrlist = new ArrayList<Notesreceivable>();
                Iterator n = ci.getNotesreceivables().iterator();
                while (n.hasNext()) {
                    Notesreceivable no = (Notesreceivable) n.next();
                    nrlist.add(no);
                }
            }


//            List<Informants> inf = new ArrayList<Informants>();
            if (ci.getInformantses() != null) {
                inflist = new ArrayList<Informants>();
                Iterator in = ci.getInformantses().iterator();
                while (in.hasNext()) {
                    Informants infor = (Informants) in.next();
                    inflist.add(infor);
                }
            }


//            List<Documentspassed> doc = new ArrayList<Documentspassed>();
            if (ci.getDocumentspasseds() != null) {
                doclist = new ArrayList<Documentspassed>();
                Iterator dp = ci.getDocumentspasseds().iterator();
                while (dp.hasNext()) {
                    Documentspassed dpassed = (Documentspassed) dp.next();
                    doclist.add(dpassed);
                }
            }


//            List<Reputation> reputations = new ArrayList<Reputation>();
            if (ci.getReputationCreditinvs() != null) {
                replist = new ArrayList<Reputation>();
                Iterator rc = ci.getReputationCreditinvs().iterator();
                while (rc.hasNext()) {
                    ReputationCreditinv repcre = (ReputationCreditinv) rc.next();
                    Reputation r = repcre.getReputation();
                    replist.add(r);
                }
            }


//            List<Sourceofincome> soi = new ArrayList<Sourceofincome>();
            if (ci.getSourceCreditinvs() != null) {
                soilist = new ArrayList<Sourceofincome>();
                Iterator isoi = ci.getSourceCreditinvs().iterator();
                while (isoi.hasNext()) {
                    SourceCreditinv sci = (SourceCreditinv) isoi.next();
                    Sourceofincome s = sci.getSourceofincome();
                    soilist.add(s);
                }
            }


//            List<Typeofcollateral> cols = new ArrayList<Typeofcollateral>();
            if (ci.getCollateralCreditinvs() != null) {
                toclist = new ArrayList<Typeofcollateral>();
                Iterator col = ci.getCollateralCreditinvs().iterator();
                while (col.hasNext()) {
                    CollateralCreditinv cci = (CollateralCreditinv) col.next();
                    Typeofcollateral cc = cci.getTypeofcollateral();
                    toclist.add(cc);
                }
            }

            //loan app lists
            Iterator it = l.getDependentses().iterator();

            while (it.hasNext()) {
                Dependents bu = (Dependents) it.next();
                deplist.add(bu);
//                System.out.println(bu.getName());
            }

            for (int i = 0; i < deplist.size(); i++) {
//                System.out.println("DEP: " + deplist.get(i));
            }

            for (int i = 0; i < lar.size(); i++) {
//                System.out.println("REQ: " + lar.get(i));
            }

            for (int i = 0; i < reflist.size(); i++) {
//                System.out.println("REF: " + reflist.get(i));
            }

            for (int i = 0; i < loanlist.size(); i++) {
//                System.out.println("LOAN: " + loanlist.get(i));
            }

            if (listcoll != null && listcoll.size() != 0) {
                ServletActionContext.getRequest().getSession().setAttribute("colllist", listcoll);
            } else {
                ServletActionContext.getRequest().getSession().setAttribute("colllist", null);
            }

            if (business != null && business.size() != 0) {
                ServletActionContext.getRequest().getSession().setAttribute("business", business);
            } else {
                ServletActionContext.getRequest().getSession().setAttribute("bus", null);
            }

            if (deplist != null && deplist.size() != 0) {
                ServletActionContext.getRequest().getSession().setAttribute("deplist", deplist);
            } else {
                ServletActionContext.getRequest().getSession().setAttribute("deplist", null);
            }
            
            if (osi != null && osi.size() != 0) {
                ServletActionContext.getRequest().getSession().setAttribute("osilist", osi);
            } else {
                ServletActionContext.getRequest().getSession().setAttribute("osilist", null);
            }

            if (reflist != null && reflist.size() != 0) {
                ServletActionContext.getRequest().getSession().setAttribute("reflist", reflist);
            } else {
                ServletActionContext.getRequest().getSession().setAttribute("reflist", null);
            }

            if (loanlist != null && loanlist.size() != 0) {
                ServletActionContext.getRequest().getSession().setAttribute("loanlist", loanlist);
            } else {
                ServletActionContext.getRequest().getSession().setAttribute("loanlist", null);
            }

            if (lar != null && lar.size() != 0) {
                reqlist = new ArrayList<Requirements>();
                for (int i = 0; i < lar.size(); i++) {
                    reqlist.add(lar.get(i).getRequirements());
//                    ("REQUIREMENTS:" + lar.get(i).getRequirements().getRequirementname());
                }
                ServletActionContext.getRequest().getSession().setAttribute("reqlist", reqlist);
            } else {
                ServletActionContext.getRequest().getSession().setAttribute("reqlist", null);
            }

            if (parplist != null && parplist.size() != 0) {
                ServletActionContext.getRequest().getSession().setAttribute("parplist", parplist);
            } else {
                ServletActionContext.getRequest().getSession().setAttribute("parplist", null);
            }

            if (soilist != null && soilist.size() != 0) {
                ServletActionContext.getRequest().getSession().setAttribute("soilist", soilist);
            } else {
                ServletActionContext.getRequest().getSession().setAttribute("soilist", null);
            }

            if (replist != null && replist.size() != 0) {
                ServletActionContext.getRequest().getSession().setAttribute("replist", replist);
            } else {
                ServletActionContext.getRequest().getSession().setAttribute("replist", null);
            }

            if (nrlist != null && nrlist.size() != 0) {
                ServletActionContext.getRequest().getSession().setAttribute("nrlist", nrlist);
            } else {
                ServletActionContext.getRequest().getSession().setAttribute("nrlist", null);
            }

            if (inflist != null && inflist.size() != 0) {
                ServletActionContext.getRequest().getSession().setAttribute("inflist", inflist);
            } else {
                ServletActionContext.getRequest().getSession().setAttribute("inflist", null);
            }

            if (doclist != null && doclist.size() != 0) {
                ServletActionContext.getRequest().getSession().setAttribute("doclist", doclist);
            } else {
                ServletActionContext.getRequest().getSession().setAttribute("doclist", null);
            }

            if (toclist != null && toclist.size() != 0) {
                ServletActionContext.getRequest().getSession().setAttribute("toclist", toclist);
            } else {
                ServletActionContext.getRequest().getSession().setAttribute("toclist", null);
            }



//            System.out.println("LOANAPPID" + loanappid);
            return SUCCESS;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }
}
